package com.auxiliary;

import com.myidis.entity.SignalThreshold;
import com.myidis.entity.TimeSequence;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;

public class SignalThresholdCalculator
{
    public SignalThresholdCalculator()
    {
    }
    /// <summary>
    /// 基于统计方法计算指标阀值
    /// </summary>
    /// <param name="ts">要计算阀值的TimeSequence对象</param>
    /// <param name="thresholdPercentList">阀值的百分比划分。是一个List<double>, count=4。按照由小到大的顺序排列。如:
    /// 0.2,0.4,0.5,0.9</param>
    /// <returns></returns>
    public SignalThreshold CalculateThresholdByStatistics(TimeSequence ts, ArrayList<Double> thresholdPercentList, int quota, String subject)
    {
        if (thresholdPercentList == null || thresholdPercentList.size() != 4 || ts==null)  return null;
        SignalThreshold threshold = new SignalThreshold();
        int count = ts.getValues().size();
        if (count == 0) return null;
        ArrayList<Double> list = ts.getValues();
        ArrayList<Integer> percentNum = new ArrayList<>();
        threshold.setSubject(subject);
        threshold.setQuota(quota);
        for (double d : thresholdPercentList)
        {
            percentNum.add((int)Math.floor(count * d));
        }
        Collections.sort(list);
        threshold.setBlueValue(list.get(percentNum.get(0)));
        threshold.setGreenValue(list.get(percentNum.get(1)));
        threshold.setYellowValue(list.get(percentNum.get(2)));
        threshold.setRedValue(list.get(percentNum.get(3)));
        return threshold;
    }

}